package com.xuelangyun.form.form.controller;

import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xuelangyun.form.common.utils.PageUtils;
import com.xuelangyun.form.common.utils.R;
import com.xuelangyun.form.common.validator.Assert;
import com.xuelangyun.form.common.validator.ValidatorUtils;
import com.xuelangyun.form.common.validator.group.AddGroup;
import com.xuelangyun.form.common.validator.group.UpdateGroup;
import com.xuelangyun.form.form.entity.DyFormTemplateEntity;
import com.xuelangyun.form.form.service.IFormTemplateService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse; 

/**
 * 动态表单模板
 *
 * @author weiqing.hk
 * @date 2024-03-30 11:19:44
 */
@RestController
@RequestMapping("form/template")
@Api(value = "动态表单模板API", tags = { "动态表单模板API-通用" })
public class DyFormTemplateController {

	@Autowired
	private IFormTemplateService IFormTemplateService;

	/**
	 * 列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("form:template:list")
	@ApiOperation("动态表单模板列表")
	@ApiResponse(code=0,message="查询成功",response= DyFormTemplateEntity.class)
	public R<PageUtils> list(DyFormTemplateEntity entity , PageUtils page){
		PageUtils pageData = IFormTemplateService.queryPage(entity , page);
		return R.ok(PageUtils.class).setData(pageData);
	}


	/**
	 * 信息
	 */
	@GetMapping("/info/{id}") 
	@ApiOperation("根据ID获取动态表单模板信息")
	@ApiResponse(code=0,message="查询成功",response= DyFormTemplateEntity.class)
	public R<DyFormTemplateEntity> info(@PathVariable("id") String id){
		DyFormTemplateEntity formTemplate = IFormTemplateService.getById(id);
		return R.ok(DyFormTemplateEntity.class).setData( formTemplate);
	}
	
	/**
	 * 信息
	 */
	@GetMapping("/queryByCode") 
	@ApiOperation("根据CODE获取动态表单模板信息")
	@ApiResponse(code=0,message="查询成功",response= DyFormTemplateEntity.class)
	public R<DyFormTemplateEntity> queryByCode(String code){
		DyFormTemplateEntity formTemplate = IFormTemplateService.queryByCode(code);
		return R.ok(DyFormTemplateEntity.class).setData( formTemplate);
	}

	/**
	 * 保存
	 */
	@PostMapping("/save")
	@RequiresPermissions("form:template:update")
	@ApiOperation("保存动态表单模板信息") 
	public R<Object> save(@RequestBody DyFormTemplateEntity entity){
		ValidatorUtils.validateEntity(entity, AddGroup.class);
		IFormTemplateService.save(entity);
		return R.ok();
	}
	
	/**
	 *  撤回暂存
	 */
	@PostMapping("/revoke")
	@RequiresPermissions("form:template:update")
	@ApiOperation("保存动态表单模板信息") 
	public R<Object> revoke(@RequestBody DyFormTemplateEntity entity){
		
		Assert.isNull(entity, "表单模板为空");
		Assert.isBlank(entity.getId(), "表单模板为空");
		IFormTemplateService.revoke(entity.getId());
		return R.ok();
	}
	
	/**
	 *  发布
	 */
	@PostMapping("/publish")
	@RequiresPermissions("form:template:update")
	@ApiOperation("发布动态表单模板信息") 
	public R<Object> publish(@RequestBody DyFormTemplateEntity entity){
		
		Assert.isNull(entity, "表单模板为空");
		Assert.isBlank(entity.getId(), "表单模板为空");
		IFormTemplateService.publish(entity.getId());
		return R.ok();
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@RequiresPermissions("form:template:update")
	@ApiOperation("修改动态表单模板信息") 
	public R<Object> update(@RequestBody DyFormTemplateEntity entity){
		ValidatorUtils.validateEntity(entity, UpdateGroup.class);
		IFormTemplateService.updateById(entity);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete")
	@RequiresPermissions("form:template:delete")
	@ApiOperation("删除动态表单模板信息") 
	public R<Object> delete(@RequestBody String[] ids){
		IFormTemplateService.removeByIds(Arrays.asList(ids));
		return R.ok();
	}

}
